/*jshint esversion: 11 */
var pre = "2";
function show(val) {
  let item = val.innerHTML;
  if (val.value) {
    item = val.value;
    document.getElementById("show-info").innerHTML += item;
  } else {
    document.getElementById("show-info").innerHTML += item;
  }
}
function cal(val) {
  if (val.id) {
    let result = document
      .getElementById("show-info")
      .innerHTML.replace(/π/, "Math.PI");
    result = eval(result.replace(/e/, "Math.E"));
    if (result) {
      document.getElementById("show-info").innerHTML = result.toFixed(+pre);
    } else {
      // alert("fuck");
      document.getElementById("show-info").innerHTML = "ERROR!";
    }
  } else {
    let result;
    if (val.value) {
      let value = val.value;

      if (value === "double") {
        result = eval(document.getElementById("show-info").innerHTML);
        document.getElementById("show-info").innerHTML = Math.pow(result, 2);
      } else if (value === "clear") {
        document.getElementById("show-info").innerHTML = "";
      } else if (value === "sqrt") {
        result = eval(document.getElementById("show-info").innerHTML);
        document.getElementById("show-info").innerHTML = Math.sqrt(result);
      } else {
        let r = document.getElementById("show-info").innerHTML;
        document.getElementById("show-info").innerHTML = r?.substring(
          0,
          r?.length - 1
        );
        if (r.length == 1) {
          document.getElementById("show-info").innerHTML = "";
        }
      }
    } else {
      let value = val.innerHTML;
      result = eval(document.getElementById("show-info").innerHTML);
      switch (value) {
        case "sin":
          document.getElementById("show-info").innerHTML = Math.sin(result);
          break;
        case "cos":
          document.getElementById("show-info").innerHTML = Math.cos(result);
          break;
        case "tan":
          document.getElementById("show-info").innerHTML = Math.tan(result);
          break;
        case "lg":
          document.getElementById("show-info").innerHTML = Math.log10(result);
          break;
        case "ln":
          document.getElementById("show-info").innerHTML = Math.log(result);
          break;
      }
    }
  }
}
function change_pre(val) {
  let r = val.value;
  pre = r;
}
function change_num() {
  let r = parseInt(document.getElementById("show-info").innerHTML);

  if (r) {
    if (r > 0) {
      if (document.getElementById("show-info").innerHTML.indexOf("+") > -1)
        document.getElementById("show-info").innerHTML = document
          .getElementById("show-info")
          .innerHTML.replace("+", "-");
      else
        document.getElementById("show-info").innerHTML =
          "+" + document.getElementById("show-info").innerHTML;
    } else if (r < 0) {
      document.getElementById("show-info").innerHTML = document
        .getElementById("show-info")
        .innerHTML.replace("-", "+");
    }
  }
}
